package com.example;

import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Twitter;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

public class TwitterClient {
	static final String TAG = "TwitterClient";
	private Twitter twitter;
	private SharedPreferences prefs;
	@SuppressWarnings("unused")
	private Context m_context;
	private final static String CONSUMER_KEY = "i6hchEkpicnjWlwoqyw";
	private final static String CONSUMER_SECRET = "yD4vxgT21yFsgMQS50Qabwvx3kIR70NhlpSa8ImI";
	private final static String USER_KEY = "27429414-CHAtID7NmAmF7UCH2F8M9ffvNlOrdZjOqyfOrfq0H";
	private final static String USER_SECRET = "YfRgfWmrqSbjAtyUIzQCHZo5WcPMLQj0mQOqPjRyo";

	public TwitterClient(Context context) {
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
		prefs
				.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
					public void onSharedPreferenceChanged(
							SharedPreferences arg0, String arg1) {
						twitter = null;
					}
				});
		m_context = context;

	}

	public Twitter getTwitter() {
		if (twitter == null) {
			OAuthSignpostClient client = new OAuthSignpostClient(CONSUMER_KEY, CONSUMER_SECRET,
					USER_KEY, USER_SECRET);

			// TODO Fix case when login doesn't work

			String username = prefs.getString("username", "");
//			String password = prefs.getString("password", "");
//
//			if (username != "" && password != "")
//				twitter = new Twitter(username, password);
//			else {
//				twitter = null;
//			}
			twitter = new Twitter(username, client);
		}
		return twitter;
	}

	public void setProxySettings() {
		if (prefs.getBoolean("proxy", false)) {
			System.setProperty("http.proxyHost", prefs.getString("proxyServer",
					""));
			System.setProperty("http.proxyPort", prefs.getString("proxyPort",
					""));
			System.setProperty("http.proxyUser", prefs.getString(
					"proxyUserName", ""));
			System.setProperty("http.proxyPassword", prefs.getString(
					"proxyPassword", ""));
		} else {
			System.setProperty("http.proxyHost", "");
			System.setProperty("http.proxyPort", "");
			System.setProperty("http.proxyUser", "");
			System.setProperty("http.proxyPassword", "");

		}
	}

}
